import cv2
import numpy as np
from scipy.interpolate import CubicSpline

sample_dot = np.load("lomo.npy")
img = cv2.imread("lenna.png")
cs_stack = []
for each in sample_dot:
    each[:,0].sort()
    each[:,1].sort()
    cs_stack.append(CubicSpline(each[:,0], each[:,1]))


def adjust_h(bgr_img, function):
    hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
    hsv_img[:,:,0] = np.array(list(map(lambda x:function(x), hsv_img[:,:,0]/180*255)))/255*180
    result = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
    return result

def adjust_s(bgr_img, function):
    hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
    hsv_img[:,:,1] = np.array(list(map(lambda x:function(x), hsv_img[:,:,1])))
    result = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
    return result

def adjust_v(bgr_img, function):
    hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
    hsv_img[:,:,2] = np.array(list(map(lambda x:function(x), hsv_img[:,:,2])))
    result = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
    return result

def adjust_b(bgr_img, function):
    result = bgr_img.copy()
    result[:,:,0] = np.array(list(map(lambda x:function(x), bgr_img[:,:,0])))
    return result

def adjust_g(bgr_img, function):
    result = bgr_img.copy()
    result[:,:,1] = np.array(list(map(lambda x:function(x), bgr_img[:,:,1])))
    return result

def adjust_r(bgr_img, function):
    result = bgr_img.copy()
    result[:,:,2] = np.array(list(map(lambda x:function(x), bgr_img[:,:,2])))
    return result

def adjust_gamma(bgr_img, function):
    result = bgr_img.copy()
    result[:,:,0] = np.array(list(map(lambda x:function(x), bgr_img[:,:,0])))
    result[:,:,1] = np.array(list(map(lambda x:function(x), bgr_img[:,:,1])))
    result[:,:,2] = np.array(list(map(lambda x:function(x), bgr_img[:,:,2])))
    return result

def adjust_dark(bgr_img):
    hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)
    hsv_img[:,:,2] = hsv_img[:,:,2] * blur /255
    result = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
    return result

def nothing(x):
    pass

mask = np.zeros((img.shape[0], img.shape[1]))
cv2.ellipse(mask, (img.shape[1]//2, img.shape[0]//2), (img.shape[1]//2, img.shape[0]//2), 0, 0, 360, 255, -1)
blur = cv2.GaussianBlur(mask,(301,301),0)

img1 = adjust_h(img, cs_stack[0])
img1 = adjust_s(img1, cs_stack[1])
img1 = adjust_v(img1, cs_stack[2])
img1 = adjust_b(img1, cs_stack[3])
img1 = adjust_g(img1, cs_stack[4])
img1 = adjust_r(img1, cs_stack[5])
img1 = adjust_gamma(img1, cs_stack[6])
img1 = adjust_dark(img1)
background = 255-np.zeros((255, 255))
result = img1.copy()
img1[:,:,2] = img1[:,:,2] * blur /255
cv2.imshow("lomo", img1)
cv2.waitKey(0)